package org.sy.lab.Java7并发编程实战.四线程执行器.十在执行器中分离任务的启动与结果的处理;

import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class ReportProcessor implements Runnable {
	private CompletionService<String> service;
	private boolean end;
	public ReportProcessor(CompletionService<String> service) {
		this.service = service;
		end = false;
	}
	@Override
	public void run() {
		while(!end) {
			try {
				Future<String> result = service.poll(20, TimeUnit.SECONDS);
				if(result != null) {
					String report = result.get();
					System.out.printf("ReportReceiver: Report Received: %s\n", report);
				}
			} catch (InterruptedException | ExecutionException e) {
				e.printStackTrace();
			}
		}
		System.out.printf("ReportSender: End\n");
	}
	public void setEnd(boolean end) {
		this.end = end;
	}
}
